﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CodeGenerator.Common
{
    public class Utils
    {
        /// <summary>
        /// 得到应用程序路径
        /// </summary>
        public static string GetAppPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        /// <summary>
        /// 判断数组是否包含某个值
        /// </summary>
        /// <param name="str"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool Contains(string[] str, string val)
        {
            int num= Array.IndexOf(str, val);
            if(num==-1)
             return false;
            else
                return true;
        }

        /// <summary>
        /// 得到一个节点的根节点
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public static TreeNode GetRoot(TreeNode node)
        {

            TreeNode node1 = node;
            while (node1!=null && node1.Parent != null)
            {
                node1 = node1.Parent;
            }
            return node1;
        }

        public static string DelLastChar(string str,string strchar)
        {
            string text = str;
            int i = text.LastIndexOf(strchar);
            if (i > 0)
            {
                text= text.Substring(0, i);
            }
            return text;
        }

        /// <summary>
        /// Pascal 规则（帕斯卡命名）
        /// 每个单词开头的字母大写
        /// </summary>
        public static string Pascal(string s)
        {
            return Regex.Replace(s, @"\b[a-z]\w+", delegate(Match match)
            {
                string v = match.ToString();
                return char.ToUpper(v[0]) + v.Substring(1);
            });
        }

        /// <summary>
        /// Camel  规则（驼峰命名）
        /// 每个单词开头的字母小写
        /// </summary>
        public static string Camel(string s)
        {
            return Regex.Replace(s, @"\b[A-Z]\w+", delegate(Match match)
            {
                string v = match.ToString();
                return char.ToLower(v[0]) + v.Substring(1);
            });
        }
    }
}
